package com.ebo.rongcloud.activitys;

import android.content.pm.ActivityInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.ebo.agora.openvcall.IBaseFragmentActivity;
import com.ebo.agora.openvcall.ui.ChatActivity;
import com.ebo.agora.propeller.Constant;

import io.agora.openvcall.R;
import utils.DensityUtil;


/**
 * Created by admin on 2017/4/7.
 */

public class ConversationActivity extends IBaseFragmentActivity implements View.OnClickListener{

    private TextView mName;
    private boolean isGroup;//是否为群组聊天

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String sId = getIntent().getData().getQueryParameter("targetId");//targetId:单聊即对方ID，群聊即群组ID
        String sName = getIntent().getData().getQueryParameter("title");//获取昵称
        isGroup = sName.equals("群组聊天");
        if (isGroup) {
            // 隐藏标题栏
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            // 隐藏状态栏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            //隐藏虚拟按键
            DensityUtil.setHideVirtualKey(getWindow());
            setContentView(R.layout.conversation_group);
        } else {
            setTheme(R.style.AppTheme);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(R.layout.conversation);
        }


        mName = (TextView) findViewById(R.id.name);
        if (!TextUtils.isEmpty(sName)) {
            mName.setText(sName);
        } else {
            //TODO 拿到id 去请求自己服务端
        }

        //白板
        if(isGroup){
            initWhiteBoard();
            _loadWhiteBroad();
            //返回键
            findViewById(R.id.closeWhiteBoard).setOnClickListener(this);
        }
    }

    void initWhiteBoard() {
        if (webView == null) {
            webView = (WebView) findViewById(R.id.wv_white);
        }
    }
    WebView webView;



    private void _loadWhiteBroad() {
        StringBuffer sb = new StringBuffer();
        sb.append(Constant.Whiteboard_Url);
        sb.append("key=");
        sb.append(this.getString(R.string.private_app_id));
        sb.append("&cname=");
        sb.append(ChatActivity.channel_name_code);
        sb.append("&role=");
        sb.append("guest");
        sb.append("&width="+ Constant.Whiteboard_W);
        sb.append("&height="+Constant.Whiteboard_H);


        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // 　　//handler.cancel(); 默认的处理方式，WebView变成空白页
                handler.proceed(); // 接受证书
                // handleMessage(Message msg); 其他处理
            }
        });
        // 这两行代码一定加上否则效果不会出现
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("gb2312");
        webView.loadUrl(sb.toString());

    }



//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode==KeyEvent.KEYCODE_BACK){
//            Lg.d(" onKeyDown "+event.toString());
//            onClickBack(null);
//            if(isGroup){
//                LoadingUtils.progressDialogShow(this);
//            }
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }


    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.closeWhiteBoard){
          finish();
        }
    }


}